package bayesian;

import java.util.ArrayList;

/**
 * @author Xu,
 * @data 2020/12/6，
 * @time 14:11
 */
public class Bayesian {
    private String calyx_len;
    private String calyx_wid;
    private String petal_len;
    private String petal_wid;
    private ArrayList<ArrayList<String>> data;
    public Bayesian(String calyx_len, String calyx_wid, String petal_len, String petal_wid, ArrayList<ArrayList<String>> data){
        this.calyx_len = calyx_len;
        this.calyx_wid = calyx_wid;
        this.petal_len = petal_len;
        this.petal_wid = petal_wid;
        this.data = data;
    }

    /**
     * 贝叶斯运算部分
     */
    public void calculate_Bayesian(){
        //获取P(Iris-setosa|Iris-versicolor|Iris-virginica)的分子————————————P(x|y)的分母
        int setosa = getNums("Iris-setosa");
        int versicolor = getNums("Iris-versicolor");
        int virginica = getNums("Iris-virginica");
        /**
	         * 对数据进行划分等级，取整数位为分级标准
	         * 花萼长度以4.x，5.x，6.x，7.x为等级
	         * 花萼宽度以2.x,3.x,4.x
	         * 花瓣长度以1.x,3.x,4.x,5.x,6.x
	         * 花瓣宽度以0.x,1.x,2.x
         */
        //获取P(x|y)的分子,0代表花萼长度，1代表花萼宽度，2代表花瓣长度，3代表花瓣宽度
        int calyx_len_setosa = getNums(0,calyx_len,"Iris-setosa");
        int calyx_wid_setosa = getNums(1,calyx_wid,"Iris-setosa");
        int petal_len_setosa = getNums(2,petal_len,"Iris-setosa");
        int petal_wid_setosa = getNums(3,petal_wid,"Iris-setosa");
        int calyx_len_versicolor = getNums(0,calyx_len,"Iris-versicolor");
        int calyx_wid_versicolor = getNums(1,calyx_wid,"Iris-versicolor");
        int petal_len_versicolor = getNums(2,petal_len,"Iris-versicolor");
        int petal_wid_versicolor = getNums(3,petal_wid,"Iris-versicolor");
        int calyx_len_virginica = getNums(0,calyx_len,"Iris-virginica");
        int calyx_wid_virginica = getNums(1,calyx_wid,"Iris-virginica");
        int petal_len_virginica = getNums(2,petal_len,"Iris-virginica");
        int petal_wid_virginica = getNums(3,petal_wid,"Iris-virginica");

        //计算概率Iris_setosa,Iris_versicolor,Iris_virginica
        double setosa_probability = (setosa/(data.size()*1.0))*(calyx_len_setosa/(setosa*1.0))
                *(calyx_wid_setosa/(setosa*1.0))*(petal_len_setosa/(setosa*1.0))*(petal_wid_setosa/(setosa*1.0));
        double versicolor_probability = (versicolor/(data.size()*1.0))*(calyx_len_versicolor/(versicolor*1.0))
                *(calyx_wid_versicolor/(versicolor*1.0))*(petal_len_versicolor/(versicolor*1.0))*(petal_wid_versicolor/(versicolor*1.0));
        double virginica_probability = (virginica/(data.size()*1.0))*(calyx_len_virginica/(virginica*1.0))
                *(calyx_wid_virginica/(virginica*1.0))*(petal_len_virginica/(virginica*1.0))*(petal_wid_virginica/(virginica*1.0));


        //比较概率得出结论
        if(setosa_probability > versicolor_probability && setosa_probability > virginica_probability){
            System.out.println("<"+calyx_len+","+calyx_wid+","+petal_len+","+petal_wid+","+">  ---> Iris-setosa");
        }
        if(versicolor_probability > setosa_probability && versicolor_probability > virginica_probability){
            System.out.println("<"+calyx_len+","+calyx_wid+","+petal_len+","+petal_wid+","+">  ---> Iris-versicolor");
        }
        if(virginica_probability > setosa_probability && virginica_probability > versicolor_probability){
            System.out.println("<"+calyx_len+","+calyx_wid+","+petal_len+","+petal_wid+","+">  ---> Iris-virginica");
        }


    }

    private int getNums(String str) {
        int num = 0;
        for(int i=0;i<data.size();i++){
            if(data.get(i).get(4).equals(str)){
                num++;
            }
        }
        return num;
    }

    private int getNums(int id, String str1, String str2){
        int num = 0;
        for(int i=0;i<data.size();i++){
            if(data.get(i).get(4).equals(str2)){
                if(data.get(i).get(id).charAt(0) == str1.charAt(0)){
                    num++;
                }
            }
        }
        return num;
    }
}
